package com.ojoin.trade.supplyloan_core.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlLoanStatTotalDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotalVo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotalVoCompare;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.service.TbSlLoanStatTotalService;

/**
 * Created with IntelliJ IDEA. User: Administrator Date: 15-10-27 Time: 上午10:09
 * To change this template use File | Settings | File Templates.
 */
@Service
public class TbSlLoanStatTotalServiceImpl extends
		BaseServiceImpl<TbSlLoanStatTotal> implements TbSlLoanStatTotalService {
	@Autowired
	private TbSlLoanStatTotalDao dao;

	@Override
	protected BaseDao<TbSlLoanStatTotal> getBaseDao() {
		return dao; // To change body of implemented methods use File | Settings
					// | File Templates.
	}

	

	@Override
	/**
	 * 查询电商平台户业务数据统计
	 * @param joinuserNo
	 * @throws BusinessException
	 */
	public List<TbSlLoanStatTotalVo> queryTbSlLoanStatTotalByJoinidAndTime(
			int joinuserNo, String time) throws BusinessException {
		return dao.queryTbSlLoanStatTotalByJoinidAndTime(joinuserNo, time);
	}

	

	@Override
	public void deleteTbSlLoanTotalByPlatformUserIDAndDate(int platformUserId,
			String statDate) throws BusinessException {
		((TbSlLoanStatTotalDao) this.getBaseDao())
				.deleteTbSlLoanTotalByPlatformUserIDAndDate(platformUserId,
						statDate);
	}

	@Override
	public List<TbSlLoanStatTotalVo> queryByJoinidandPlatform(
			int idOfTbMerchantUser, int idOfPlatformUser, Pageable page)
			throws BusinessException {
		return ((TbSlLoanStatTotalDao) this.getBaseDao())
				.queryLoanTotalByJoinidandPlatform(idOfTbMerchantUser,
						idOfPlatformUser, page);
	}

	@Override
	public List<TbSlLoanStatTotalVo> queryByPlatformAndMerchantUserIDAndDate(
			String platformName, String merchantUserId, String statDate)
			throws BusinessException {
		Map<String, Object> paramap = new HashMap<String, Object>();
		
		paramap.put("platformName", platformName);
		paramap.put("merchantUserId", merchantUserId);
		paramap.put("statDate", statDate);
		List<TbSlLoanStatTotalVo> li = dao
				.queryByPlatformAndMerchantUserIDAndDate(paramap);

		return li;
	}

	@Override
	public TbSlLoanStatTotal getTbSlLoanStatTotalVoByPlatformUser(
			TbSlPlatformUser platformUser) {
		TbSlLoanStatTotal total = new TbSlLoanStatTotal();
		total.setIdOfPlatformUser(platformUser.getId());
		total.setIdOfTbMerchantUser(platformUser.getIdOfTbMerchantUser());
		total.setPlatformName(platformUser.getPlatformName());
		total.setStatEndDate(TimeUtils.getCurDate());

		return total;
	}

	@Override
	public List<TbSlLoanStatTotalVoCompare> queryCompareByPlatformAndMerchantUserIDAndDate(
			String platformName, String merchantUserId, String startDate,
			String endDate, Pageable page) throws BusinessException {

		if (CommonUtils.isEmpty(endDate) && CommonUtils.isEmpty(startDate)) {
			throw new BusinessException("您输入的时间有误");
		} else {
			try {
				if (TimeUtils.getDateSpaceOfDay(startDate, endDate) <= 0) {
					throw new BusinessException("开始时间必须比结束时间小");
				}
			} catch (Exception e) {
				throw new BusinessException("时间转化出错");
			}
		}

		Map<String, Object> paramap = new HashMap<String, Object>();
		paramap.put("platformName", platformName);
		paramap.put("merchantUserId", merchantUserId);
		
		
		paramap.put("timebefore", startDate);
		paramap.put("timenow", endDate);
		
		List<TbSlLoanStatTotalVoCompare> li = dao
				.queryCompareByPlatformAndMerchantUserIDAndDate(paramap, page);
		return li;
	}

	@Override
	public TbSlLoanStatTotal queryByPlateIdAndTime(String plateId, String time) {
		return dao.queryByPlateIdAndTime(plateId, time);
	}



	@Override
	public String getLastInsertTime(int platformAccountId) {
		return dao.getLastInsertTime(platformAccountId);
	}

	
}
